package com.zhangye.test.api;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ArComment;
import com.zhangye.test.model.ArReport;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.ReportType;
import com.zhangye.test.service.ArCommentService;
import com.zhangye.test.service.ArReportService;
import com.zhangye.test.service.EasyArService;
import com.zhangye.test.service.ReportTypeService;
import com.zhangye.test.service.SafeSessionService;

/**
 * 举报
 * 作者： 张叶
 * 创建时间：2018年8月29日  上午11:30:35
 */
@Controller
@RequestMapping("api/report")
public class ArReportApi {
	
	@Autowired
	private ArReportService arReportService;
	@Autowired
	private SafeSessionService safeSessionService;
	@Autowired
	private ReportTypeService reportTypeService;
	@Autowired
	private EasyArService easyArService;
	@Autowired
	private ArCommentService arCommentService;


	/**
	 * 拉黑作品
	 * 作者： 张叶
	 * 创建时间：2018年8月31日  上午9:50:28
	 */
	@ResponseBody
	@RequestMapping(value = "/defriend", method = RequestMethod.POST)
	public ApiResult defriend(HttpServletRequest request,Long easyArId) {
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(easyArId != null && !"".equals(easyArId)){
				EasyAr ea = easyArService.selectByPrimaryKey(easyArId);
				if(ea != null && !"".equals(ea)){
					String notlike = ea.getNotLikeAdminId();
					if(notlike != null && !"".equals(notlike)){
						ea.setNotLikeAdminId(notlike + admin.getId() + ",");
					}else{
						ea.setNotLikeAdminId("," + admin.getId() + ",");
					}
					int i = easyArService.updateNormal(ea);
					if(i > 0){
						result.msg.status = 0;
						result.msg.desc = "操作成功";
					} else {
						result.msg.status = 1;
						result.msg.desc = "系统错误，操作失败";
					}
				} else {
					result.msg.status = 4;
					result.msg.desc = "该作品或已被删除，请刷新后重试";
				}
			} else {
				result.msg.status = 3;
				result.msg.desc = "参数不能为空";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}


	/**
	 * 获取举报类型
	 * 作者： 张叶
	 * 创建时间：2018年10月26日  下午3:21:48
	 */
	@ResponseBody
	@RequestMapping(value = "/type", method = RequestMethod.GET)
	public ApiResult type(Integer type) {
		ApiResult result = new ApiResult();
		if(type != null && !"".equals(type)){
			String where = " and type = " + type;
			List<ReportType> list = reportTypeService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("reportType", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}


	/**
	 * 添加举报
	 * 作者： 张叶
	 * 创建时间：2018年8月29日  上午11:33:48
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ApiResult insert(HttpServletRequest request,Long contentId,Integer type,Long reportTypeId) {
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(reportTypeId != null && !"".equals(reportTypeId) &&
					contentId != null && !"".equals(contentId) &&
					type != null && !"".equals(type)){
				//判断举报作品或评论存在
				ReportType rt = reportTypeService.selectByPrimaryKey(reportTypeId);
				if(rt != null && !"".equals(rt)){
					if(type == rt.getType()){//举报ID与举报类型匹配
						int flag = 0;
						if(type == 0){//作品
							EasyAr ea = easyArService.selectByPrimaryKey(contentId);
							if(ea != null && !"".equals(ea)){//添加举报
								flag = 1;
							}else{
								result.msg.status = 7;
								result.msg.desc = "该作品或已被删除，刷新后重试";
							}
						}else if(type == 1){
							ArComment ac = arCommentService.selectById(contentId);
							if(ac != null && !"".equals(ac)){//添加举报
								flag = 1;
							}else{
								result.msg.status = 7;
								result.msg.desc = "该评论或已被删除，刷新后重试";
							}
						}else{
							result.msg.status = 4;
							result.msg.desc = "举报类型不正确";
						}
						if(flag == 1){
							int i = insertReport(contentId, reportTypeId, type,admin.getId());
							if(i > 0){
								result.msg.status = 0;
								result.msg.desc = "举报成功";
							}else{
								result.msg.status = 1;
								result.msg.desc = "系统错误，举报失败";
							}
						}
					}else{
						result.msg.status = 6;
						result.msg.desc = "举报类别与举报类型不匹配";
					}
				}else{
					result.msg.status = 5;
					result.msg.desc = "举报类别不正确";
				}
			}else{
				result.msg.status = 3;
				result.msg.desc = "参数不能为空";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}


	private int insertReport(Long contentId, Long reportTypeId,Integer type, Long adminId) {
		ArReport r = new ArReport();
		r.setAdminId(adminId);
		r.setEasyArId(contentId);
		r.setReportTypeId(reportTypeId);
		r.setCreatedAt(new Date());
		r.setStatus("audit");
		int i = arReportService.insert(r);
		return i;
	}
}
